<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          type="primary"
          @click="handleSearchList()"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="输入搜索：">
            <el-input
              v-model="listQuery.batchName"
              class="input-width"
              placeholder="活动名称"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        size="mini"
        class="btn-add"
        @click="handleAddSku"
        style="margin-left: 20px"
        >添加</el-button
      >
    </el-card>
    <div class="table-container">
      <el-table
        ref="roleTable"
        :data="list"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column label="活动名称" align="center" prop="batchName">
        </el-table-column>
        <el-table-column label="产品名称" align="center" prop="productName">
        </el-table-column>

        <el-table-column label="优惠券有效期开始时间" align="center">
          <template slot-scope="scope">{{
            scope.row.termBeginTime | formatDateTime
          }}</template>
        </el-table-column>
        <el-table-column label="优惠券有效期结束时间" align="center">
          <template slot-scope="scope">{{
            scope.row.termEndTime | formatDateTime
          }}</template>
        </el-table-column>
        <el-table-column label="优惠券有效期天数" align="center">
          <template slot-scope="scope">{{ scope.row.termDays }}</template>
        </el-table-column>
        <el-table-column label="数量" align="center">
          <template slot-scope="scope">{{ scope.row.batchNumber }}</template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.isDeleted ? 'danger' : 'success'">{{
              scope.row.isDeleted ? "已删除" : "未删除"
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center">
          <template slot-scope="scope">{{
            scope.row.updatedAt | formatDateTime
          }}</template>
        </el-table-column>
        <el-table-column label="备注" align="center">
          <template slot-scope="scope">{{ scope.row.remark }}</template>
        </el-table-column>
        <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope">
            <el-row>
              <router-link
                :to="{
                  name: 'amsCoupon',
                  query: {
                    id: scope.row.id,
                  },
                }"
                tag="a"
              >
                <el-button size="mini" type="text">查看优惠券</el-button>
              </router-link>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="PAGE_SIZES"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { formatDate } from "@/utils/date";
import { fetchActivityList } from "@/api/ams";
import { publishStatus, publishStatusEnum } from "@/utils/constant/pms";
import ModalForm from "@/components/ModalForm.vue";
import { PAGE_SIZES, DEFAULT_PAGE_SIZE } from "@/utils/constant/system";

const defaultListQuery = {
  pageNum: 1,
  pageSize: DEFAULT_PAGE_SIZE,
  batchName: "",
};
const defaultRole = {
  id: null,
  name: null,
  description: null,
  adminCount: 0,
  status: 1,
};
export default {
  name: "roleList",
  components: {
    ModalForm,
  },
  data() {
    return {
      PAGE_SIZES,
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: false,
      dialogVisible: false,
      role: Object.assign({}, defaultRole),
      isEdit: false,
      publishStatus,
      publishStatusEnum,
      metaForm: {},
      userForm: {
        username: "",
        email: "",
      },
      formRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  filters: {
    formatDateTime(time) {
      if (!time) {
        return "";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    },
  },
  methods: {
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleAddSku() {
      this.$router.push({
        name: "amsAddActivity",
      });
    },

    getList() {
      this.listLoading = true;
      fetchActivityList(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
  },
};
</script>
<style></style>
